// pages/shopManager/goodsManager/goodsEdit/goodsEdit.js
const common = require('../../../../utils/util.js')
const md5 = require('../../../../utils/md5.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsId: null,
        goodsType: null,
        goodsMainImg: '',
        goodsName: '',
        goodsBrief: '',
        goodsClassifyList: ['饰品', '食品'],
        goodsClassifyIndex: null,
        goodsFormat: [{
            color: '',
            img: '',
            price: '',
            factory_price: '',
            sku_num: ''
        }],
        goodsBonus: '',
        goodsSubImg: [],
        goodsDetailImg: [],
        shopIntro: false,
        onSale: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.goodsId) {
            this.setData({
                goodsId: options.goodsId,
                goodsType: options.type
            })
        } else {
            this.setData({
                goodsType: options.type
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    goodsNameInput: function(e) {
        this.setData({
            goodsName: e.detail.value
        })
    },

    goodsBriefInput: function(e) {
        this.setData({
            goodsBrief: e.detail.value
        })
    },

    goodsClassifyChange: function(e) {
        this.setData({
            goodsClassifyIndex: e.detail.value
        })
    },

    formatNameInput: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat[e.currentTarget.dataset.index].color = e.detail.value
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    formatFactoryPriceInput: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat[e.currentTarget.dataset.index].factory_price = e.detail.value
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    formatPriceInput: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat[e.currentTarget.dataset.index].price = e.detail.value
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    formatStockInput: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat[e.currentTarget.dataset.index].sku_num = e.detail.value
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    goodsBonusInput: function(e) {
        this.setData({
            goodsBonus: e.detail.value
        })
    },

    addFormat: function() {
        let goodsFormat = this.data.goodsFormat
        let formatData = {
            color: '',
            img: '',
            price: '',
            factory_price: '',
            sku_num: ''
        }
        goodsFormat.push(formatData)
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    deleteFormat: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    deleteSubImg: function(e) {
        let goodsSubImg = this.data.goodsSubImg
        goodsSubImg.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            goodsSubImg: goodsSubImg
        })
    },

    deleteDetailImg: function(e) {
        let goodsDetailImg = this.data.goodsDetailImg
        goodsDetailImg.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            goodsDetailImg: goodsDetailImg
        })
    },

    shopIntroChange: function(e) {
        console.log(e.detail.value)
    },

    onSaleChange: function(e) {
        console.log(e.detail.value)
    },

    uploadImg: function(e) {
        const that = this
        let data = {}
        let key = wx.getStorageSync('token')
        let sign = 'mozi'
        let timestamp = Date.parse(new Date())
        data.key = key
        data.timestamp = timestamp
        data = this.objKeySort(data)
        for (let i in data) {
            sign += (i + data[i])
        }
        sign += 'mozi'
        sign = md5(sign)
        data.sign = sign
        wx.chooseImage({
            count: e.currentTarget.dataset.type == 'mainImg' || e.currentTarget.dataset.type == 'formatImg' ? 1 : 9, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    title: '正在上传图片',
                })
                let tempFilePaths = res.tempFilePaths
                if (e.currentTarget.dataset.type == 'mainImg') {
                    wx.uploadFile({
                        url: 'https://mozi.bailingyoupin.com/api/Upimg/up_major_img', //仅为示例，非真实的接口地址
                        filePath: tempFilePaths[0],
                        name: 'file',
                        formData: {
                            key: data.key,
                            timestamp: data.timestamp,
                            sign: data.sign
                        },
                        success(res) {
                            wx.hideLoading()
                            if (JSON.parse(res.data).code == 204) {
                                wx.showModal({
                                    title: '提示',
                                    content: JSON.parse(res.data).error,
                                    showCancel: false
                                })
                                return
                            }
                            wx.showToast({
                                title: '上传图片成功',
                                icon: 'none',
                                duration: 2000
                            })
                            that.setData({
                                goodsMainImg: JSON.parse(res.data).datas
                            })
                        }
                    })
                } else if (e.currentTarget.dataset.type == 'formatImg') {
                    wx.uploadFile({
                        url: 'https://mozi.bailingyoupin.com/api/Upimg/up_sku_img', //仅为示例，非真实的接口地址
                        filePath: tempFilePaths[0],
                        name: 'file',
                        formData: {
                            key: data.key,
                            timestamp: data.timestamp,
                            sign: data.sign
                        },
                        success(res) {
                            wx.hideLoading()
                            if (JSON.parse(res.data).code == 204) {
                                wx.showModal({
                                    title: '提示',
                                    content: JSON.parse(res.data).error,
                                    showCancel: false
                                })
                                return
                            }
                            wx.showToast({
                                title: '上传图片成功',
                                icon: 'none',
                                duration: 2000
                            })
                            let goodsFormat = that.data.goodsFormat
                            goodsFormat[e.currentTarget.dataset.index].img = JSON.parse(res.data).datas
                            that.setData({
                                goodsFormat: goodsFormat
                            })
                        }
                    })
                } else if (e.currentTarget.dataset.type == 'subImg') {
                    tempFilePaths.forEach(function(item, i) {
                        wx.uploadFile({
                            url: 'https://mozi.bailingyoupin.com/api/Upimg/up_vice_img', //仅为示例，非真实的接口地址
                            filePath: item,
                            name: 'file',
                            formData: {
                                key: data.key,
                                timestamp: data.timestamp,
                                sign: data.sign
                            },
                            success(res) {
                                if (JSON.parse(res.data).code == 204) {
                                    wx.hideLoading()
                                    wx.showModal({
                                        title: '提示',
                                        content: JSON.parse(res.data).error,
                                        showCancel: false
                                    })
                                    return
                                }
                                let goodsSubImg = that.data.goodsSubImg
                                goodsSubImg.push(JSON.parse(res.data).datas)
                                that.setData({
                                    goodsSubImg: goodsSubImg
                                })
                                if (i + 1 == tempFilePaths.length) {
                                    wx.hideLoading()
                                    wx.showToast({
                                        title: '上传图片成功',
                                        icon: 'none',
                                        duration: 1000
                                    })
                                }
                            }
                        })
                    })
                } else if (e.currentTarget.dataset.type == 'detailImg') {
                    tempFilePaths.forEach(function(item, i) {
                        wx.uploadFile({
                            url: 'https://mozi.bailingyoupin.com/api/Upimg/up_info_img', //仅为示例，非真实的接口地址
                            filePath: item,
                            name: 'file',
                            formData: {
                                key: data.key,
                                timestamp: data.timestamp,
                                sign: data.sign
                            },
                            success(res) {
                                if (JSON.parse(res.data).code == 204) {
                                    wx.hideLoading()
                                    wx.showModal({
                                        title: '提示',
                                        content: JSON.parse(res.data).error,
                                        showCancel: false
                                    })
                                    return
                                }
                                let goodsDetailImg = that.data.goodsDetailImg
                                goodsDetailImg.push(JSON.parse(res.data).datas)
                                that.setData({
                                    goodsDetailImg: goodsDetailImg
                                })
                                if (i + 1 == tempFilePaths.length) {
                                    wx.hideLoading()
                                    wx.showToast({
                                        title: '上传图片成功',
                                        icon: 'none',
                                        duration: 1000
                                    })
                                }
                            }
                        })
                    })
                }
            }
        })
    },

    objKeySort: function(obj) {
        let newkey = Object.keys(obj).sort()
        let newObj = {}
        for (let i = 0; i < newkey.length; i++) {
            newObj[newkey[i]] = obj[newkey[i]]
        }
        return newObj
    }
})